﻿using JRCore.protocol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WHYHLWebservices
{
    public partial class Service1 : ServiceBase
    {
        public string gYHLUri = null;
        public string gFTPfolder = null;
        public static YHLWebService m_yhlWebServ = new YHLWebService();
        //获取EHL的webservice接口，并保存到本地
        public static YHLWebServiceGetter m_yhlWebServGetter = new YHLWebServiceGetter();
        //webservice是否可以连接
        public static bool m_bIsInitWebServ = false;
        public static PicCenterPush push = null;
        public Service1()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            gYHLUri = ConfigurationManager.AppSettings["YHLUri"];
            gFTPfolder = ConfigurationManager.AppSettings["FTPfolder"];

            if (!Directory.Exists(gFTPfolder)) Directory.CreateDirectory(gFTPfolder);
            fileSystemWatcher1.Path = gFTPfolder;
            //push = GetNewData();
            //初始化web接口
            if (m_yhlWebServGetter.GetWebServiceLocal())
            {
                m_yhlWebServGetter.InitMethod();
                m_bIsInitWebServ = true;
                LogHelper.WriteLog(typeof(Service1), "获取EHL接口成功，已初始化！");
            }
            else
            {
                LogHelper.WriteLog(typeof(Service1), "获取EHL接口失败，请检查网络连接！");
            }
        }

        protected override void OnStart(string[] args)
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞数据上传易华录服务启动");

        }

        protected override void OnStop()
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞数据上传易华录服务停止");
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            string filename = System.IO.Path.GetFileNameWithoutExtension(e.FullPath);//文件名  
            string[] s = filename.Split(new char[] { '-' });

            DataRecvRecord record = new DataRecvRecord();
            FullDataRecvRecord(record, s, e);           
            PushtoYHL();
        }
        //填充DataRecvRecord
        private void FullDataRecvRecord(DataRecvRecord record, string[] s, FileSystemEventArgs e)
        {
            record.P15 = s[0];
            switch (s[1])
            {
                case "东向西":
                    record.P16 = "1";
                    break;
                case "南向北":
                    record.P16 = "2";
                    break;
                case "西向东":
                    record.P16 = "3";
                    break;
                case "北向南":
                    record.P16 = "4";
                    break;
                default:
                    record.P16 = "1";
                    break;
            }
            record.P17 = s[2];
            record.P27 = s[3];
            record.P26 = s[4];

            if (s[6].Contains("无牌"))
            {
                record.P23 = "";
            }
            else
            {
                record.P23 = s[6];
            }
            switch (s[7])
            {
                case "大车":
                    record.P21 = "01";
                    break;
                case "小车":
                    record.P21 = "02";
                    break;
                default:
                    record.P21 = "99";
                    break;
            }
            record.P24 = "0";
            if (s[9].Contains("未知"))
            {
                record.P22 = "";
            }
            else
            {
                record.P22 = s[9];
            }
            record.P5 = s[10];
            record.P13 = s[11];

            record.P8 = s[12];
            record.P18 = s[13];
            record.P30 = e.FullPath;
            push = PicCenterPush.Parse(record.P);
        }

        //上传易华录
        private void PushtoYHL()
        {
            try
            {
                string jrResult = "0";
                if (push.P5 == "2")
                {
                    jrResult = "1";
                }
                else
                {
                    String strYHLData = YHLUtil.MakeXml(push);
                    if (strYHLData.Length > 0)
                    {

                        LogHelper.WriteLog(typeof(Service1), "发送数据并发送" + push.P23 + "--" + push.P18 + "--" + push.P15 + "长度:" + strYHLData.Length);

                        //在可访问的情况下进行发送
                        if (m_bIsInitWebServ)
                        {
                            string result = m_yhlWebServGetter.SendPassCarInfo(strYHLData);

                            if (result.Length > 0 && result == "OK") //成功
                            {
                                jrResult = "1";
                                LogHelper.WriteLog(typeof(Service1), "发送EHL数据" + push.P23 + "--" + push.P18 + "--" + push.P15 + "长度:" + strYHLData.Length);

                            }
                            else if (result.Length > 0)
                            {
                                LogHelper.WriteLog(typeof(Service1), "EHL上传返回:" + result);
                            }
                            else
                            {
                                LogHelper.WriteLog(typeof(Service1), "返回EHL结果为空");
                            }
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(typeof(Service1), "EHL数据为空:出现异常");

                    }
                }
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex.Message);

            }
        }
    }
}
